package com.wingscode.modules.app.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wingscode.common.utils.PageUtils;
import com.wingscode.common.utils.R;
import com.wingscode.common.validator.ValidatorUtils;
import com.wingscode.modules.app.form.*;
import com.wingscode.modules.common.entity.*;
import com.wingscode.modules.common.service.*;
import com.wingscode.modules.sys.controller.AbstractController;
import com.wingscode.modules.sys.entity.VipLevelEntity;
import com.wingscode.modules.sys.service.VipLevelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 我的企业
 *
 * @author yan
 */
@RestController
@RequestMapping("/app/2")
@Api("我的企业")
public class AppController2 extends AbstractController {
    @Autowired
    private UserService userService;
    @Autowired
    private AgentService agentService;
    @Autowired
    private SecurityCodeService securityCodeService;
    @Autowired
    private CheckLogService checkLogService;
    @Autowired
    private VipLevelService vipLevelService;

    @PostMapping("/saveUser")
    @ApiOperation("新增员工")
    public R saveUser(@RequestBody SaveUserForm form) {
        //表单校验
        ValidatorUtils.validateEntity(form);

        if (getSecurityCodeEntity(form.getBizId()).getCode().equals(form.getSecurityCode())) {
            UserEntity userEntity = new UserEntity();
            userEntity.setUsername(form.getUsername());
            userEntity.setMobile(form.getMobile());
            userEntity.setPosition(form.getPosition());
            userEntity.setIsOfferPrice(form.getIsOfferPrice());
            userEntity.setAgentId(form.getAgentId());
            userEntity.setStatus(1);

            if (form.getUserId() == null) {
                userEntity.setCreateTime(new Date());
                userEntity.setUpdateTime(new Date());
                userService.save(userEntity);
            } else {
                userEntity.setId(form.getUserId());
                userEntity.setUpdateTime(new Date());
                userService.updateById(userEntity);
            }
        } else {
            return R.error("验证码错误");
        }

        return R.ok();
    }

    @GetMapping("/changeUserStatus")
    @ApiOperation("改变员工状态")
    public R changeUserStatus(@RequestParam Integer userId, @RequestParam Integer status) {
        if (userId == null)
            return R.error("userId为空");
        if (status == null)
            return R.error("status为空");

        UserEntity userEntity = userService.getById(userId);
        if (userEntity != null) {
            userEntity.setStatus(status);
            userEntity.setUpdateTime(new Date());

            userService.updateById(userEntity);
        }

        return R.ok();
    }

    @GetMapping("/deleteUser")
    @ApiOperation("删除员工")
    public R deleteUser(@RequestParam Integer userId) {
        if (userId == null)
            return R.error("userId为空");

        userService.deleteById(userId);

        return R.ok();
    }

    @PostMapping("/getUserList")
    @ApiOperation("员工管理list")
    public R getUserList(@RequestBody Map<String, Object> params) {
        if (!params.containsKey("agentId"))
            return R.error("agentId为空");

        PageUtils page = userService.getUserList(params);

        return R.ok(200, page);
    }

    @GetMapping("/getCompanyInfo")
    @ApiOperation("企业信息")
    public R getCompanyInfo(@RequestParam Integer agentId) {
        if (agentId == null)
            return R.error("agentId为空");

        AgentEntity agent = agentService.getById(agentId);
        // 企业审核信息
        List<CheckLogEntity> list = checkLogService.list(
                new QueryWrapper<CheckLogEntity>()
                        .eq("type", 1)
                        .eq("agent_id", agentId)
                        .orderByDesc("create_time")
        );
        if (list != null && list.size() > 0)
            agent.setCheckLog(list.get(0));
        // 企业会员等级信息
        if (agent != null) {
            VipLevelEntity vipLevelEntity = vipLevelService.getById(agent.getLevel());
            if (vipLevelEntity != null)
                agent.setLevelName(vipLevelEntity.getName());
        }

        return R.ok(200, agent);
    }

    private SecurityCodeEntity getSecurityCodeEntity(String bizId) {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.MINUTE, 10);
        Date d1 = c.getTime();
        return securityCodeService.getOne(new QueryWrapper<SecurityCodeEntity>().eq("bizid", bizId).le("gmt_create", d1));
    }

}
